<template>
  <el-card>
    <el-form :model="adminInfo" ref="editForm" :rules="editRules" label-width="120px">
      <el-form-item label="角色" prop="roleid">
        <el-select v-model="adminInfo.roleid" placeholder="请选择父级">
          <el-option label="请选择" value=""></el-option>
          <el-option v-for="item in roles" :label="item.rolename" :value="item.id" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="管理账号" prop="username">
        <el-input placeholder="请输入登录账号" v-model="adminInfo.username"></el-input>
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input type="password" placeholder="不修改密码可不填" v-model="adminInfo.password" :show-password="true" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="adminInfo.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submit('editForm')">确认修改</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      adminInfo: {
          uid:this.$route.params.id,
          roleid:'',
          username:'',
          password:'',
          status:''
      },
      editRules: {
        roleid: { required: true, message: "请选择角色", trigger: "blur" },
        username: {
          required: true,
          message: "请输入登录账号",
          trigger: "blur"
        },
        status: { required: true, message: "请选择账号状态", trigger: "blur" }
      },
      roles: []
    };
  },
  created(){
      this.getRoleList();
      this.getAdminInfo();
  },
  methods:{
      async getRoleList(){
          const res=await this.$http.get('/api/rolelist');
          if(res.code==200){
              this.roles=res.list;
          }
      },
      async getAdminInfo(){
          const res=await this.$http.get('/api/userinfo',{params:{uid:this.adminInfo.uid}});
          if(res.code==200){
            this.adminInfo=res.list; 
            this.adminInfo.password='';
          }
      },
      submit(form){
        this.$refs[form].validate(async valid=>{
          if(valid){
            const res=await this.$http.post('/api/useredit',this.adminInfo);
            console.log(res);
            if(res.code==200){
               return this.$success('操作成功');
            }
            this.$error('操作失败');
          }else{
            console.log('表单校验未通过');
          }
        })
      }  
  }
};
</script>

<style>
</style>